package org.jeecg.common.util.wx;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author guoyh
 * @ClassName WxUrlConvert
 * @Description
 * @since 2020/9/5 18:15
 **/
@Slf4j
public class WxUrlConvert {

    public static String convertUrl(String redirect,String appid){

        try {
            redirect = URLEncoder.encode(redirect,"UTF-8");
            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=DEFAULT&connect_redirect=1#wechat_redirect";
            log.info("WxUrlConvert："+url);
            return String.format(url,appid,redirect);
        } catch (UnsupportedEncodingException e) {
            log.error("转换异常",e);
            return null;
        }
    }

    public static String buildChatUrl(String h5ApiDomain,String h5Domain,String groupId,String doctorId,String appid){
        String redirect = String.format("%s/auth?url=%s/chat/%s?id=%s!!!token=",h5ApiDomain,h5Domain,groupId, doctorId);
        return convertUrl(redirect,appid);
    }

    public static String encode(String url){
        try {
            return URLEncoder.encode(url,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("转换异常{}",url,e);
            return null;
        }
    }
}
